let ua = window.navigator.userAgent.toLowerCase();
var iswx = ''
if (ua.match(/MicroMessenger/i) == 'micromessenger' && ua.match(/WxWork/i) != "wxwork") {
	iswx = 'wx'
}
var phone = '';
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
	phone = 'ios';
} else if (/(Android)/i.test(navigator.userAgent)) {  //判断Android
	phone = 'Android';
} else { //pc
};


// 监听error 函数
function handleErrorFn(e) {
	// 跳转失败
	// alert(JSON.stringify(e)+ '跳转失败')
	window.location.href = `https://www.yiqilaiwang.com/h5openapp2/app.html`;
}
// 监听launch 函数
function handleLaunchFn(e) {
	// 跳转成功
	// alert(e+'跳转成功')
}
function geturl(jumpType, serviceId, type, orgId, content) {
	let url = `yqlw://yilaiwang.com/openDetail?jumpType=${jumpType}&serviceId=${serviceId}&type=${type ? type : ''}&orgId=${orgId ? orgId : ''}&content=${content ? content : ''}`
	if (iswx == 'wx') {
		var launchBtn = document.getElementById('launch-btn')
		launchBtn.setAttribute('extinfo', url)
	}
}
function detectVersion() {
	var isAndroid = false, isIOS = false, isIOS9 = false, version = "";
	var u = navigator.userAgent, ua = u.toLowerCase();

	if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {//android终端或者uc浏览器
		isAndroid = true
	} else {
		if (ua.indexOf("like mac os x") > 0) {//ios
			var regStr_saf = /os [\d._]*/gi;
			var verinfo = ua.match(regStr_saf);
			version = (verinfo + "").replace(/[^0-9|_.]/ig, "").replace(/_/ig, ".");
		}

		var version_str = version + "";
		if (version_str != "undefined" && version_str.length > 0) {
			version = parseInt(version)
			if (version >= 10) {// ios9以上
				isIOS9 = true
			}
			else {
				isIOS = true
			}
		}
	}

	return { isAndroid, isIOS, isIOS9 }
}

function toApps(jumpType, serviceId, type, orgId, content) {
	var t0 = Date.now();
	type = type || 0;
	var vi = detectVersion();
	if (!vi.isAndroid) {
		// ios
		var url = `https://app.yiqilaiwang.com/pageJump?jumpType=${jumpType}&serviceId=${serviceId ? serviceId : ''}&type=${type ? type : ''}&orgId=${orgId ? orgId : ''}&content=${content ? content : ''}`
	} else {
		// 安卓
		var url = `yqlw://yilaiwang.com/openDetail?jumpType=${jumpType}&serviceId=${serviceId ? serviceId : ''}&type=${type ? type : ''}&orgId=${orgId ? orgId : ''}&content=${content ? content : ''}`
	}
	if (vi.isAndroid || vi.isIOS) {//安卓手机，或苹果手机9以下版
		var t1 = Date.now();
		var hasApp = true;
		var ifr = document.createElement("iframe");
		ifr.setAttribute('src', url);
		ifr.setAttribute('style', 'display:none');//使用隐藏的iframe来启动外部APP
		document.body.appendChild(ifr);
		setTimeout(function () {//3秒后检查是否启动成功，否则打开下载APP页面
			if (hasApp == false) {
				window.location.href = vi.isIOS ? "https://apps.apple.com/cn/app/id1473793767" : "http://app.mi.com/details?id=com.yiqilaiwang";
			}
		}, 2500);

		setTimeout(function () {//2秒后检查是否启动了APP
			var t2 = Date.now();
			if (t2 - t1 < 2100) {//如果成功启动外部APP，会消耗系统时间，让这个定时器不能准时触发，t2-t1会大于2100
				hasApp = false;
			}
		}, 1500);
	} else if (vi.isIOS9) {//苹果手机9版
		window.location.href = url;
		window.event.returnValue = false;
		setTimeout(function () {//没有安装app，跳到下载APP页面
			window.location.href = "https://apps.apple.com/cn/app/id1473793767";
		}, 100);

		setTimeout(function () {//启动APP成功，重载本页
			window.location.reload();
		}, 1000);
	}
}
export default {
	iswx,
	phone,
	handleErrorFn,
	handleLaunchFn,
	geturl,
	toApps
}